package com.yc3.system;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Container
 * @since: 2023/7/26 08:57
 * @auth: kirito
 * @description:
 **/
@Component
public class Container<T> {
    private List<T> objs = new ArrayList<T>();

    //spring 完成DI
    @Resource(name="bmimeasure")
    private Measure measure;
    @Resource(name="bmifilter")
    private ContainerFilter filter;

    private T max;
    private T min;
    private double avg;
    private double sum;

    /**
     * 添加对象方法
     * @param t
     */
    public void add(T t) {
        /*判断t是否合格 调用筛选的实现
        * 添加到objs
        * 判断大小 记录 max min 计算avg 调用measure的实例*/
        if (filter != null) {
            if (filter.doFilter(t) == false) {
                return;
            }
        }
        objs.add(t);
        if (objs.size() == 1) {
            max = t;
            min = t;
        } else {
            double val = this.measure.doMeasure(t);
            double maxval = this.measure.doMeasure(max);
            double minval = this.measure.doMeasure(min);
            if (val > maxval) {
                max = t;
            }
            if (val < minval) {
                min = t;
            }
        }
        sum += measure.doMeasure(t);
        avg = sum/objs.size();


    }

    /**
     * 对象多少
     * @return
     */
    public int size() {
        return objs.size();
    }

    public void clearAll() {
        objs = new ArrayList<T>();
        measure = null;
        filter = null;
        max = null;
        min = null;
        avg = 0;
    }

    public T getMax() {
        return max;
    }

    public T getMin() {
        return min;
    }

    public double getAvg() {
        return avg;
    }
}
